var url = require('url');
var https = require('https');
var crypto = require("crypto");
//应用id
var appid = "1523960651";
//应用密钥
var appsecret = "ac90bb1eba4a4e94b7057da9e9f61b0d";
//加密常量
var text = "[Lucifer]";
//时间戳
var timestamp = Math.round(new Date().getTime() /1000);
//加密规则
var rule = appid+appsecret+text+timestamp+appid;
var md5  = crypto.createHash("md5");
md5.update(rule);
//计算签名
var sign = md5.digest('hex');
//请求参数
var data = {
    appid: appid,
    timestamp: timestamp,
    sign: sign
};

//组装参数
var jdata = JSON.stringify(data);
//计算长度
var len = Buffer.byteLength(jdata, 'utf8');
var weburl = 'https://game.royal-lucifer.top/API/Token/AccessToken';
var urldata = url.parse(weburl);
//HTTP请求选项
var opt = {
  hostname: urldata.hostname,
  path: urldata.path,
  method: 'POST',
  headers: {
      'Content-Type': 'application/json',
      'Content-Length': len
  }
};

var req = https.request(opt,function(res){
  var buffers = [];
  res.on('data', function(chunk) {
      buffers.push(chunk);
  });

  res.on('end', function(chunk) {
      var resdata = Buffer.concat(buffers);
      var xdata = resdata.toString('utf8');
      console.log('\n请求结果: \n ' + xdata+"\n");
  });
}).on('error', function(error) {
  console.log('error ' + error);
});
console.log("\n请求参数:\n"+jdata);
req.write(jdata);
req.end();
